define(["../../Core/Color", "../../Scene/LabelStyle", "../../Scene/HorizontalOrigin", "../../Scene/VerticalOrigin", "../../Core/ScreenSpaceEventHandler", "../../Core/ScreenSpaceEventType", "../../Scene/PointPrimitiveCollection", "../../Scene/BlendOption", "../../Core/Math", "../../Core/GeometryInstance", "../../Core/GroundPolylineGeometry", "../../Core/EllipseGeometry", "../../Core/Cartesian3", "../../Core/ColorGeometryInstanceAttribute", "../../Scene/GroundPrimitive", "../../Scene/GroundPolylinePrimitive", "../../Scene/PolylineColorAppearance", "../../Core/DistanceDisplayConditionGeometryInstanceAttribute", "./support/PickGlobe", "../../Scene/HeightReference", "../../Core/Ellipsoid", "../../Core/Math", "../../Core/Cartesian2", "../../Core/defined",], function (y, l, p, j, u, g, b, h, t, s, m, i, n, v, k, f, e, x, d, c, r, q, o, a) {
    function w(B) {
        this.viewer = B;
        this.scene = this.viewer.scene;
        this.path = [];
        this.lines = [];
        this.label = [];
        this.pickGlobe = new d(B);
        this.pixelPoints = [];
        this.lastPoint = null;
        this.totalLength = 0;
        this.tempLine = null;
        this.tempLabel = null;
        this.elementEvent = undefined;
        this.point = this.viewer.entities.add({point: {color: y.RED, pixelSize: 10}});
        this.labelTotle = this.viewer.entities.add({
            label: {
                show: false,
                showBackground: true,
                font: "36px Helvetica",
                fillColor: y.WHITE,
                outlineColor: y.BLACK,
                outlineWidth: 1,
                style: l.FILL_AND_OUTLINE,
                horizontalOrigin: p.CENTER,
                verticalOrigin: j.BASELINE,
                disableDepthTestDistance: 10000,
                scale: 0.5
            }
        });
        var z = this;
        this.clickDraw = new u(this.scene.canvas);
        this.clickDraw.setInputAction(function (I) {
            var H = z.pickGlobe.pickGlobe(I.position);
            z.lastPoint = H;
            var J = z.scene.globe.ellipsoid.cartesianToCartographic(H);
            var P = z.path, F = J && J.longitude ? t.toDegrees(J.longitude) : null,
                K = J && J.latitude ? t.toDegrees(J.latitude) : null, N = J && J.height ? J.height : null, G = 0, E = 0;
            if (N < -50) {
                return
            }
            z.tempLabel && z.viewer.entities.remove(z.tempLabel);
            P.push(H);
            var M = P.length, D = z.scene.primitives.add(new k(new s({
                geometry: new i({
                    center: H,
                    semiMinorAxis: 5,
                    semiMajorAxis: 5
                }), attributes: {color: new v.fromColor(y.RED.withAlpha(0.8))}
            })));
            z.pixelPoints.push(D);
            if (M > 1) {
                var O = z.scene.groundPrimitives.add(new f({
                    geometryInstances: new s({
                        geometry: new m({
                            positions: [P[M - 1], P[M - 2]],
                            width: 4
                        }), attributes: {color: v.fromColor(y.RED.withAlpha(0.7))}
                    })
                }));
                z.lines.push(O);
                E = n.distance(P[M - 1], P[M - 2]);
                z.totalLength = z.totalLength + E.toFixed(2) * 1;
                z.labelTotle.position = P[0];
                z.labelTotle.show = true;
                z.labelTotle.label.show = true;
                z.labelTotle.label.text = "总长:" + z.totalLength.toFixed(2) + "米";
                var C = new n(0, 0, 0);
                C.x = (P[M - 1].x + P[M - 2].x) / 2;
                C.y = (P[M - 1].y + P[M - 2].y) / 2;
                C.z = (P[M - 1].z + P[M - 2].z) / 2;
                var L = z.viewer.entities.add({
                    show: true,
                    position: C,
                    label: {
                        show: true,
                        text: E.toFixed(2) + "米",
                        showBackground: true,
                        font: "36px Helvetica",
                        fillColor: y.WHITE,
                        outlineColor: y.BLACK,
                        outlineWidth: 1,
                        style: l.FILL_AND_OUTLINE,
                        horizontalOrigin: p.CENTER,
                        verticalOrigin: j.BASELINE,
                        disableDepthTestDistance: 10000,
                        scale: 0.5,
                        heightReference: c.CLAMP_TO_GROUND,
                        eyeOffset: new n(0, 3, 0)
                    }
                });
                z.label.push(L)
            }
            z.viewer.scene.requestRenderMode = false;
            M !== 1 && document.body.removeChild(document.getElementById("mybutton"));
            M !== 1 && z.viewer.scene.postRender.removeEventListener(z.elementEvent);
            A(I)
        }, g.LEFT_CLICK);
        this.moveDraw = new u(this.scene.canvas);
        this.moveDraw.setInputAction(function (E) {
            var D = z.pickGlobe.pickGlobe(E.endPosition);
            if (D && z.lastPoint) {
                z.tempLine && z.scene.groundPrimitives.remove(z.tempLine);
                z.tempLabel && z.viewer.entities.remove(z.tempLabel);
                z.tempLine = z.scene.groundPrimitives.add(new f({
                    geometryInstances: new s({
                        geometry: new m({
                            positions: [D, z.lastPoint],
                            width: 4
                        }), attributes: {color: v.fromColor(y.RED.withAlpha(0.7))}
                    })
                }));
                var F = {x: (D.x + z.lastPoint.x) / 2, y: (D.y + z.lastPoint.y) / 2, z: (D.z + z.lastPoint.z) / 2};
                var C = n.distance(D, z.lastPoint);
                z.tempLabel = z.viewer.entities.add({
                    show: true,
                    position: F,
                    label: {
                        show: true,
                        text: C.toFixed(2) + "米",
                        showBackground: true,
                        font: "36px Helvetica",
                        fillColor: y.WHITE,
                        outlineColor: y.BLACK,
                        outlineWidth: 1,
                        style: l.FILL_AND_OUTLINE,
                        horizontalOrigin: p.CENTER,
                        verticalOrigin: j.BASELINE,
                        disableDepthTestDistance: 10000,
                        scale: 0.5,
                        heightReference: c.CLAMP_TO_GROUND,
                        eyeOffset: new n(0, 3, 0)
                    }
                });
                z.labelTotle.label.text = "总长:" + (z.totalLength * 1 + C.toFixed(2) * 1).toFixed(2) + "米"
            }
            z.point && (z.point.position = D)
        }, g.MOUSE_MOVE);

        function A(F) {
            var D = z.viewer.scene.camera.getPickRay(F.position);
            var G = z.viewer.scene.globe.pick(D, z.viewer.scene);
            var C = r.WGS84.cartesianToCartographic(G);
            var E = document.createElement("input");
            E.type = "button";
            E.id = "mybutton";
            E.value = "确定";
            E.style.position = "absolute";
            document.body.appendChild(E);
            E.onclick = function () {
                z.clickDraw && !z.clickDraw.isDestroyed() && z.clickDraw.destroy();
                z.moveDraw && !z.moveDraw.isDestroyed() && z.moveDraw.destroy();
                document.body.removeChild(E);
                z.viewer.scene.postRender.removeEventListener(z.elementEvent)
            };
            z.elementEvent = function () {
                var H = n.fromDegrees(q.toDegrees(C.longitude), q.toDegrees(C.latitude), C.height);
                var J = new o();
                var I = z.viewer.scene.cartesianToCanvasCoordinates(H, J);
                if (a(I)) {
                    E.style.top = (I.y + 10) + "px";
                    E.style.left = (I.x + 10) + "px"
                }
            };
            z.viewer.scene.postRender.addEventListener(z.elementEvent)
        }
    }

    w.prototype.clear = function () {
        var z = this;
        this.viewer.entities.remove(this.labelTotle);
        this.viewer.entities.remove(this.point);
        this.scene.groundPrimitives.remove(this.tempLine);
        this.viewer.entities.remove(this.tempLabel);
        this.label.forEach(function (A) {
            z.viewer.entities.remove(A)
        });
        this.lines.forEach(function (A) {
            z.scene.groundPrimitives.remove(A)
        });
        this.clickDraw && !this.clickDraw.isDestroyed() && this.clickDraw.destroy();
        this.moveDraw && !this.moveDraw.isDestroyed() && this.moveDraw.destroy();
        this.point = null;
        this.path = [];
        this.lines = [];
        var E = document.getElementById('mybutton');
        E && document.body.removeChild(E);
        this.elementEvent && this.viewer.scene.postRender.removeEventListener(this.elementEvent)
    };
    return w
});